

public class Main {
    public static void main(String[] args) {
        int[] arr={1,0,2,3,0,4,5,0};
        Main main = new Main();
        main.duplicateZeros(arr);
    }

    public void moveZeroes(int[] nums) {
        int dest=-1;
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=0){
                mySwap(nums,i,++dest);
            }
        }
    }

    private void mySwap(int[] num, int num1,int num2) {
        int temp=num[num1];
        num[num1]=num[num2];
        num[num2]=temp;
    }

    public void duplicateZeros(int[] arr) {
        // 1.先找到复写位置
        int curr=0;
        int dest=-1;
        int n=arr.length;
        while(curr<n)
        {
            if(arr[curr]==0){
                dest+=2;
            }else{
                dest++;
            }
            if(dest>=n-1)break;
            curr++;
        }
        // 2. 处理边界问题
        if(dest==n){
            arr[n-1]=0;
            curr--;
            dest-=2;
        }
        // 3.开始操作
        while(curr>=0){
            if(arr[curr]!=0){
                arr[dest--]=arr[curr--];
            }else{
                arr[dest--]=0;
                arr[dest--]=0;
                curr--;
            }
        }
    }

    public int sum(int n){
        int sum=0;
        while(n!=0){
            int t=n%10;
            sum+=t*t;
            n=n/10;
        }
        return sum;
    }
    public boolean isHappy(int n) {
        int fast=sum(n);int slow=n;
        while(fast!=slow){
            fast=sum(sum(fast));
            slow=sum(slow);
        }
        return slow==1;
    }

}